/**

 */
package Servlet;

import Acquaintance.IAHouse;
import Acquaintance.IASearchType;
import Control.CCache;
import Control.CIndex;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * Search——Servlet <br>
 * 负责对属性数据的查询<br>
 * 必要参数：keyword<br>
 * 返回类型为JSONArray<EHouse><br>
 * 不限定关键字类型和长度<br>
 * 只有关键字与房屋属性字段有相同或相似<br>
 * 则返回结果<br>
 * 
 * @author moe
 */
public class Search extends HttpServlet {

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		CCache cache = CCache.getCache();
		CIndex index = CIndex.getIndex();
		try {
			if (!request.getParameterMap().containsKey("keyword"))
				out.print("{'Status':'ERROR'}");
			else {
				String keyword = request.getParameter("keyword");
				keyword = new String(keyword.getBytes("ISO8859-1"), "UTF-8");

				// String keyword =
				// URLDecoder.decode(request.getParameter("keyword"), "UTF-8");

				ArrayList<Integer> result = index.search(keyword);
				JSONArray json = new JSONArray();
				JSONObject SearchType = new JSONObject();
				for (int HID : result) {
					switch (HID) {
					case IASearchType.FLOOR:
						SearchType.accumulate("SearchType", "FLOOR");
						;
						break;
					case IASearchType.AREA:
						SearchType.accumulate("SearchType", "AREA");
						;
						break;
					case IASearchType.PRICE:
						SearchType.accumulate("SearchType", "PRICE");
						;
						break;
					case IASearchType.LOCATION:
						SearchType.accumulate("SearchType", "LOCATION");
						;
						break;
					case IASearchType.TYPE:
						SearchType.accumulate("SearchType", "TYPE");
						;
						break;
					case IASearchType.PRMISE:
						SearchType.accumulate("SearchType", "PRMISE");
						;
						break;
					default:
						;
						break;
					}
					IAHouse house = (IAHouse) cache.getHouseMap().get(
							"HID" + HID);
					json.add(house);
				}

				json.add(SearchType);
				out.print(json);
			}
		} finally {
			out.close();
		}
	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		response.setContentType("text/html;charset=UTF-8");
		// request.setCharacterEncoding("UTF-8");
		CCache cache = CCache.getCache();
		CIndex index = CIndex.getIndex();
		try {

			BufferedReader reader = request.getReader();

			StringBuilder buffer = new StringBuilder();

			String string;
			while ((string = reader.readLine()) != null) {
				//string = new String(string.getBytes("ISO8859-1"), "UTF-8");

				buffer.append(string);
			}
			reader.close();

			JSONObject param = JSONObject.fromObject(buffer.toString());

			// String keyword =
			// URLDecoder.decode(request.getParameter("keyword"), "UTF-8");

			ArrayList<Integer> result = index
					.search(param.getString("keyword"));
			System.out.println(param);
			JSONArray json = new JSONArray();
			JSONObject SearchType = new JSONObject();
			
			for (int HID : result) {
				IAHouse house = null;
				switch (HID) {
				case IASearchType.FLOOR:
					SearchType.accumulate("SearchType", "FLOOR");
					;
					break;
				case IASearchType.AREA:
					SearchType.accumulate("SearchType", "AREA");

					;
					break;
				case IASearchType.PRICE:
					SearchType.accumulate("SearchType", "PRICE");

					;
					break;
				case IASearchType.LOCATION:
					SearchType.accumulate("SearchType", "LOCATION");
					;
					break;
				case IASearchType.TYPE:
					SearchType.accumulate("SearchType", "TYPE");

					;
					break;
				case IASearchType.PRMISE:
					SearchType.accumulate("SearchType", "PRMISE");

					;
					break;
				default:
					house = (IAHouse) cache.getHouseMap().get("HID" + HID);
					;
					break;
				}
				if(house!=null)
					json.add(house);	
			}
			
			json.add(SearchType);

			// out.print(new String(json.toString().getBytes("UTF-8"),
			// "UTF-8"));
			out.print(new String(json.toString().getBytes("UTF-8"), "ISO8859-1"));
			// out.write(new String(json.toString().getBytes("UTF-8"),
			// "ISO8859-1"));
			// out.write(SearchType);

		} finally {
			out.close();
		}

	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>
}
